package com.imooc.miaosha.web;

import com.imooc.miaosha.domain.User;
import com.imooc.miaosha.redis.RedisService;
import com.imooc.miaosha.redis.UserKey;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class DemoController {

	@Autowired
	UserService userService;
	@Autowired
	RedisService redisService;

	@RequestMapping("hello")
	public String hello(Model model) {
		model.addAttribute("name", "hello");
		return "hello";
	}

	@ResponseBody
	@RequestMapping("getUser")
	public Result getUser() {
		User user = userService.getUser(1);
		return Result.sucess(user);
	}

	@ResponseBody
	@RequestMapping("insertUser")
	public Result insertUser(String name) {
		int id = userService.insert(name);
		return Result.sucess(id);
	}

	@ResponseBody
	@RequestMapping("get/r/")
	public Result getRedis(String name) {

		Integer name1 = redisService.get(UserKey.getById,"name", int.class);
		return Result.sucess(name1);
	}
}
